const {food} = require('../../utils/config')
import {getHotList} from "../../utils/request";

const {getDetail, searchResult} = require('../../utils/request')

Page({

    data: {
        keyword: '',
        hotList: [],//热榜
        resultList: [],//搜索结果
        searchSwitch: true,//搜索框
        select: false,//判断有没有选择
        defaultCompare: {},//默认数据
        selectionCompare: {}//选择数据
    },
    //搜索
    getResult(event) {
        this.setData({
            keyword: event.detail.value,
        })

        if (event.detail.value === '') return

        searchResult(food, event.detail.value).then(res => {
            this.setData({
                resultList: res.data
            })
        })
    },
    //选择数据
    selectionData(event) {
        const {id} = event.currentTarget.dataset
        getDetail(food, id).then(res => {
            this.setData({
                selectionCompare: res.data,
                select: true,
                searchSwitch: true,
                resultList: [],
                keyword: ''
            })
        })
    },
    //取消搜索
    cancelSearch() {
        this.setData({
            searchSwitch: true
        })
    },
    // 点击，显示搜索框
    selection() {
        this.setData({
            searchSwitch: false
        })
    },
    //取消选择
    cancelSelection() {
        this.setData({
            select: false,
            selectionCompare: {}
        })
    },

    onLoad(event) {
        const {id} = event
        getDetail(food, id).then(res => {
            this.setData({
                defaultCompare: res.data
            })
        })
        getHotList(food).then(res => {
            this.setData({
                hotList: res.data
            })
        })

    },

})